package com.bootdo.shop.dao.provider;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.bootdo.shop.domain.OrderDetail;
import com.bootdo.shop.domain.OrderInfo;
import com.bootdo.shop.domain.request.OrderInfoReq;

/** 
 * @author 作者 : chenxuan
 * @version 创建时间：2016年1月6日 下午4:52:57 
 * 类说明 
 */
public class OrderMapperProvider {

	public String batchCreateOrderDetail(Map<String,Object>  map){
		//@Param("orderDetails") List<OrderDetail> orderDetails,@Param("orderId") Integer orderId
		StringBuffer sb = new StringBuffer();
		List<OrderDetail> orderDetails = (List<OrderDetail>)map.get("orderDetails");
		sb.append("insert into order_detail(order_id,goods_id,specification_id,goods_name,goods_batch_number,sale_price,real_price,goods_number,product_id,is_real,is_service,"
				+ "market_price,type,thumbnail_image,service_tag_type,discount_rate,discount_amount,stage_months,stage_interest_rate,stage_amount,subsidy_rate,subsidy_amount,coupon_amount) values");
	    MessageFormat messageFormat = new MessageFormat("(#'{'orderId},#'{'orderDetails[{0}].goodsId},#'{'orderDetails[{0}].specificationId},#'{'orderDetails[{0}].goodsName},#'{'orderDetails[{0}].goodsBatchNumber},"
	    		+ "#'{'orderDetails[{0}].salePrice},#'{'orderDetails[{0}].realPrice},#'{'orderDetails[{0}].goodsNumber},"
	    		+ "#'{'orderDetails[{0}].productId},#'{'orderDetails[{0}].isReal},#'{'orderDetails[{0}].isService},#'{'orderDetails[{0}].marketPrice},#'{'orderDetails[{0}].type},"
	    		+ "#'{'orderDetails[{0}].thumbnailImage},#'{'orderDetails[{0}].serviceTagType},#'{'orderDetails[{0}].discountRate},#'{'orderDetails[{0}].discountAmount},#'{'orderDetails[{0}].stageMonths},"
	    		+ "#'{'orderDetails[{0}].stageInterestRate},#'{'orderDetails[{0}].stageAmount},#'{'orderDetails[{0}].subsidyRate},#'{'orderDetails[{0}].subsidyAmount},#'{'orderDetails[{0}].couponAmount})");
		int len = orderDetails.size();
		for(int i=0;i<len;i++){
			sb.append(messageFormat.format(new Integer[]{i}));
            sb.append(",");
	    }
	    sb.setLength(sb.length() - 1);
		return sb.toString();
	}
	public String updateOrderAddress(OrderInfoReq orderInfoReq){
		StringBuffer sbBuffer = new StringBuffer();
		sbBuffer.append("update order_info set id = id ");
		if(orderInfoReq.getReceiver() != null){
			sbBuffer.append(",receiver = #{receiver}");
		}
		if(orderInfoReq.getProvince() != null){
			sbBuffer.append(",province = #{province}");
		}
		if(orderInfoReq.getCity() != null){
			sbBuffer.append(",city = #{city}");
		}
		if(orderInfoReq.getDistrict() != null){
			sbBuffer.append(",district = #{district}");
		}
		if(orderInfoReq.getAddress() != null){
			sbBuffer.append(",address = #{address}");
		}
		if(orderInfoReq.getPostcode() != null){
			sbBuffer.append(",postcode = #{postcode}");
		}
		if(orderInfoReq.getTel() != null){
			sbBuffer.append(",tel = #{tel}");
		}
		if(orderInfoReq.getMobile() != null){
			sbBuffer.append(",mobile = #{mobile}");
		}
		sbBuffer.append(" where id = #{id}");
		return sbBuffer.toString();
	}
	
	public String createOrderInfo(OrderInfo orderInfo){
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("insert into order_info (order_no,user_id,order_status,receiver,province,city,district,postcode,address,tel,");
		sBuffer.append( "mobile,goods_number,order_payee,shipping_payee,total_payee,coupon_id,coupon_amount,is_invoice,invoice_id,invoice_amount,");
		sBuffer.append("confirm_time,pay_status,buyer_memo,expiration_time,order_title,order_img,pay_time,source,pay_type,create_time,discount_amount,stage_amount,subsidy_amount");
		sBuffer.append(",financial_programme_id,thirdparty_order_no,service_mobile,fr_amount)");
		sBuffer.append(" value (");
		sBuffer.append("#{orderNo},#{userId},#{orderStatus},#{receiver},#{province},#{city},#{district},#{postcode},#{address},#{tel},");
		sBuffer.append( "#{mobile},#{goodsNumber},#{orderPayee},#{shippingPayee},#{totalPayee},#{couponId},#{couponAmount},#{isInvoice},#{invoiceId},#{invoiceAmount},");
		sBuffer.append( "current_timestamp(),#{payStatus},#{buyerMemo},#{expirationTime},#{orderTitle},#{orderImg},#{payTime},#{source},#{payType},#{createTime},#{discountAmount},#{stageAmount},#{subsidyAmount}");
		sBuffer.append(",#{financialProgrammeDetailId},#{thirdpartyOrderNo},#{serviceMobile},#{frAmount})");
		return sBuffer.toString();
	}
	
	public String userOrderList(OrderInfoReq orderInfoReq){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from (select a.id,a.order_title,b.order_status_view order_status,a.order_no,a.order_img,"
				+ "a.create_time,a.total_payee,1 orderType,'' description,a.goods_number,a.expiration_time,a.pay_status,"
				+ "a.is_comment,a.pay_type,a.shiipping_sn,a.shipping_channel,c.name shipping_channel_name,c.code shipping_channel_code,"
				+ "a.shipping_payee,b.order_status_name,a.order_payee,a.coupon_amount,a.address,a.receiver,a.mobile,a.user_id "
				+ "from order_info a "
				+ "left join order_status_config b ON a.order_status = b.order_status AND a.pay_status = b.pay_status AND "
				+ " a.shipping_status = b.shipping_status and a.pay_type = b.pay_type left join shipping_channels c on a.shipping_channel = c.id  "
				+ " where  a.pay_type != 4 and ((a.pay_status = 1) or (a.pay_status = 0 and a.expiration_time >=  NOW() )) "
				+ " ) t where t.order_status not in (-1,-2,-3) ");
		if(orderInfoReq.getUserId() != null && orderInfoReq.getUserId() != 0) {
			sb.append(" and user_id = #{userId} ");
		}
		if(orderInfoReq.getOrderStatus() != null){
			sb.append(" and t.order_status = #{orderStatus} ");
			if(orderInfoReq.getOrderStatus() == 3){
				sb.append(" and t.is_comment != 1 ");
			}
		}
		if(StringUtils.isNotEmpty( orderInfoReq.getOrderNo())) {
			sb.append(" and t.order_no like concat('%',#{orderNo},'%') ");
		}
		if(StringUtils.isNotEmpty(orderInfoReq.getReceiver())) {
			sb.append(" and t.receiver like concat('%',#{receiver},'%') ");
		}
		if(StringUtils.isNotEmpty(orderInfoReq.getMobile())) {
			sb.append(" and t.mobile like concat('%',#{mobile},'%') ");
		}
		sb.append(" order by t.id desc ");
		if(orderInfoReq.getStart() != null){
			sb.append(" limit #{start},#{limit}");
		}
		return sb.toString();
	}
	
	public String userOrderListCount(OrderInfoReq orderInfoReq){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from (select a.id,a.order_title,b.order_status_view order_status,a.order_no,a.order_img,"
				+ "a.create_time,a.total_payee,1 orderType,'' description,a.goods_number,a.expiration_time,a.pay_status,"
				+ "a.is_comment,a.pay_type,a.shiipping_sn,a.shipping_channel,c.name shipping_channel_name,c.code shipping_channel_code,"
				+ "a.shipping_payee,b.order_status_name,a.order_payee,a.coupon_amount,a.address,a.receiver,a.mobile,a.user_id "
				+ "from order_info a "
				+ "left join order_status_config b ON a.order_status = b.order_status AND a.pay_status = b.pay_status AND "
				+ " a.shipping_status = b.shipping_status and a.pay_type = b.pay_type left join shipping_channels c on a.shipping_channel = c.id  "
				+ " where  a.pay_type != 4 and ((a.pay_status = 1) or (a.pay_status = 0 and a.expiration_time >=  NOW() )) "
				+ " ) t where t.order_status not in (-1,-2,-3) ");
		if(orderInfoReq.getUserId() != null && orderInfoReq.getUserId() != 0) {
			sb.append(" and user_id = #{userId} ");
		}
		if(orderInfoReq.getOrderStatus() != null){
			sb.append(" and t.order_status = #{orderStatus} ");
			if(orderInfoReq.getOrderStatus() == 3){
				sb.append(" and t.is_comment != 1 ");
			}
		}
		if(StringUtils.isNotEmpty( orderInfoReq.getOrderNo())) {
			sb.append(" and t.order_no like concat('%',#{orderNo},'%') ");
		}
		if(StringUtils.isNotEmpty(orderInfoReq.getReceiver())) {
			sb.append(" and t.receiver like concat('%',#{receiver},'%') ");
		}
		if(StringUtils.isNotEmpty(orderInfoReq.getMobile())) {
			sb.append(" and t.mobile like concat('%',#{mobile},'%') ");
		}
		return sb.toString();
	}
	
	public String getOrderListForOrderNoList(HashMap<String, Object> param){
		List<Object> orderNoList = (ArrayList<Object>)param.get("orderNoList");
		StringBuffer sb = new StringBuffer();
		MessageFormat messageFormat = new MessageFormat("#'{'list[{0}]}");
		sb.append("select * from order_info where order_no in ");
		sb.append("(");
		int len = orderNoList.size();
		for(int i=0;i<len;i++){
			sb.append(messageFormat.format(new Object[]{i}));
			sb.append(",");
		}
		sb.setLength(sb.length() - 1);
		sb.append(")");
		return sb.toString();
	}
	
}